package com.smartpower.wattmeter.ui;

import android.bluetooth.BluetoothGatt;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.lifecycle.Observer;

import com.android.basiclib.base.BaseActivity;
import com.android.basiclib.uitls.CheckUtil;
import com.android.basiclib.uitls.CommUtils;
import com.android.basiclib.uitls.Log.LogUtil;
import com.android.basiclib.uitls.NetWorkUtil;
import com.android.basiclib.uitls.ToastUtils;
import com.android.basiclib.view.LoadingDialogManager;
import com.android.basiclib.view.titlebar.EasyTitleBar;
import com.clj.fastble.BleManager;
import com.clj.fastble.data.BleDevice;
import com.google.gson.Gson;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.smartpower.wattmeter.Constants;
import com.smartpower.wattmeter.MyApplication;
import com.smartpower.wattmeter.R;
import com.smartpower.wattmeter.bean.DetailBean;
import com.smartpower.wattmeter.bean.EditBean;
import com.smartpower.wattmeter.manager.FastBleManager;
import com.smartpower.wattmeter.manager.MyFastBleCallback;

/**
 * 设备详情展示
 */
public class DeviceDetailActivity extends BaseActivity {

    private EasyTitleBar mEasyTitle;
    private String mJsonStr;
    private TextView mTvTxmId;
    private TextView mTvTxmType;
    private TextView mTvTxmName;
    private TextView mTvTxmDesin;
    private TextView mTvTxmRowCole;
    private TextView mTvTxmLon;
    private TextView mTvTxmLat;
    private TextView mTvBdzNum;
    private TextView mTvByqNum;
    private TextView mTvTqNum;
    private TextView mTvXlNum;
    private TextView mTvUserNum;
    private android.widget.LinearLayout mBoxTxmId;
    private android.widget.LinearLayout mBoxTxmType;
    private android.widget.LinearLayout mBoxTxmName;
    private android.widget.LinearLayout mBoxTxmDesin;
    private android.widget.LinearLayout mBoxTxmRowCole;
    private android.widget.LinearLayout mBoxTxmLon;
    private android.widget.LinearLayout mBoxTxmLat;
    private android.widget.RelativeLayout mBoxBdzNum;
    private android.widget.RelativeLayout mBoxByqNum;
    private android.widget.RelativeLayout mBoxTqNum;
    private android.widget.RelativeLayout mBoxXlNum;
    private android.widget.LinearLayout mBoxUserNum;

    private DetailBean mDetailBean;

    private final int type_zcbh = 1;
    private final int type_zclx = 2;
    private final int type_sbmc = 3;
    private final int type_sbcz = 4;
    private final int type_hlz = 5;
    private final int type_jd = 6;
    private final int type_wd = 7;
    private final int type_bdz = 8;
    private final int type_byq = 9;
    private final int type_tq = 10;
    private final int type_xl = 11;
    private final int type_yh = 12;

    private boolean hasAddRightLayout = false;


    public static void startInstance(String jsonStr) {
        Context context = CommUtils.getContext();
        Intent intent = new Intent(context, DeviceDetailActivity.class);
        intent.putExtra("jsonStr", jsonStr);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    protected void getDataFromIntent(Intent intent) {
        mJsonStr = intent.getStringExtra("jsonStr");
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_device_detail;
    }

    @Override
    protected void init() {
        initView();
        initData();
        initListener();
    }

    //展示读取的数据
    private void initData() {
        if (CheckUtil.isEmpty(mJsonStr)) {
            //测试数据
            mJsonStr = "{\"hlz\": \"1x2\",\"byq\": \"7bd5351cb88a7073a6526b631e01527bd50cc82b29\",\"bdz\": \"0621426280\",\"yh\": \"1024839204\",\"xl\": \"0001222716\",\"wd\": \"30.870162644668973\",\"tq\": \"0001355947\",\"jd\": \"114.37234841525762\",\"zclx\": \"单体表箱\",\"zcbh\": \"1712020968\",\"sbmc\": \"计量箱\",\"sbcz\": \"热镀锌金属\"\n" +
                    "}";
        }
        mDetailBean = MyApplication.getGson().fromJson(mJsonStr, DetailBean.class);
        showData2View();

    }

    //展示数据
    private void showData2View() {
        if (mDetailBean != null) {
            mTvTxmId.setText(mDetailBean.zcbh);
            mTvTxmType.setText(mDetailBean.zclx);
            mTvTxmName.setText(mDetailBean.sbmc);
            mTvTxmDesin.setText(mDetailBean.sbcz);
            mTvTxmRowCole.setText(mDetailBean.hlz);
            mTvTxmLon.setText(mDetailBean.jd);
            mTvTxmLat.setText(mDetailBean.wd);
            mTvBdzNum.setText(mDetailBean.bdz);
            mTvByqNum.setText(mDetailBean.byq);
            mTvTqNum.setText(mDetailBean.tq);
            mTvXlNum.setText(mDetailBean.xl);
            mTvUserNum.setText(mDetailBean.yh);
        }
    }

    private void initListener() {

        mBoxTxmId.setOnClickListener(v -> {
            jump2EditPage(1, mTvTxmId.getText().toString());
        });
        mBoxTxmType.setOnClickListener(v -> {
            jump2EditPage(2, mTvTxmType.getText().toString());
        });
        mBoxTxmName.setOnClickListener(v -> {
            jump2EditPage(3, mTvTxmName.getText().toString());
        });
        mBoxTxmDesin.setOnClickListener(v -> {
            jump2EditPage(4, mTvTxmDesin.getText().toString());
        });
        mBoxTxmRowCole.setOnClickListener(v -> {
            jump2EditPage(5, mTvTxmRowCole.getText().toString());
        });
        mBoxBdzNum.setOnClickListener(v -> {
            jump2EditPage(8, mTvBdzNum.getText().toString());
        });
        mBoxByqNum.setOnClickListener(v -> {
            jump2EditPage(9, mTvByqNum.getText().toString());
        });
        mBoxTqNum.setOnClickListener(v -> {
            jump2EditPage(10, mTvTqNum.getText().toString());
        });
        mBoxXlNum.setOnClickListener(v -> {
            jump2EditPage(11, mTvXlNum.getText().toString());
        });
        mBoxUserNum.setOnClickListener(v -> {
            jump2EditPage(12, mTvUserNum.getText().toString());
        });

    }

    //跳转编辑
    private void jump2EditPage(int type, String text) {
        DeviceEditActivity.startInstance(type, text);
    }

    @Override
    public void onNetworkConnectionChanged(boolean isConnected, NetWorkUtil.NetworkType networkType) {
    }

    @Override
    protected void dataObserver() {
        //写入成功的通知
        LiveEventBus.get().with(Constants.EVENT_BLE_WRITE_CALLBACK, Boolean.class).observe(this, aBoolean -> {
            if (aBoolean) {
                LoadingDialogManager.get().dismissLoading();
            }
        });

        //编辑成功的通知
        LiveEventBus.get().with(Constants.EVENT_DEVICE_EDIT, EditBean.class).observe(this, editBean -> {
            int mType = editBean.type;
            String content = editBean.text;

            if (mType == 1) {
                mDetailBean.zcbh = content;
            } else if (mType == 2) {
                mDetailBean.zclx = content;
            } else if (mType == 3) {
                mDetailBean.sbmc = content;
            } else if (mType == 4) {
                mDetailBean.sbcz = content;
            } else if (mType == 5) {
                mDetailBean.hlz = content;
            } else if (mType == 6) {
                mDetailBean.jd = content;
            } else if (mType == 7) {
                mDetailBean.wd = content;
            } else if (mType == 8) {
                mDetailBean.bdz = content;
            } else if (mType == 9) {
                mDetailBean.byq = content;
            } else if (mType == 10) {
                mDetailBean.tq = content;
            } else if (mType == 11) {
                mDetailBean.xl = content;
            } else if (mType == 12) {
                mDetailBean.yh = content;
            }

            showData2View();

            if (!hasAddRightLayout) {
                mEasyTitle.addRightText("确定写入", () -> {
                    //确定更新，写入数据
                    String writeJson = MyApplication.getGson().toJson(mDetailBean);
                    LogUtil.e("写入数据：" + writeJson);

                    LoadingDialogManager.get().showBigLoading(mActivity);
                    LiveEventBus.get().with(Constants.EVENT_BLE_WRITE).post(writeJson);
                });
                hasAddRightLayout = true;
            }


        });
    }

    private void initView() {
        mEasyTitle = findViewById(R.id.easy_title);

        mTvTxmId = findViewById(R.id.tv_txm_id);
        mTvTxmType = findViewById(R.id.tv_txm_type);
        mTvTxmName = findViewById(R.id.tv_txm_name);
        mTvTxmDesin = findViewById(R.id.tv_txm_desin);
        mTvTxmRowCole = findViewById(R.id.tv_txm_row_cole);
        mTvTxmLon = findViewById(R.id.tv_txm_lon);
        mTvTxmLat = findViewById(R.id.tv_txm_lat);
        mTvBdzNum = findViewById(R.id.tv_bdz_num);
        mTvByqNum = findViewById(R.id.tv_byq_num);
        mTvTqNum = findViewById(R.id.tv_tq_num);
        mTvXlNum = findViewById(R.id.tv_xl_num);
        mTvUserNum = findViewById(R.id.tv_user_num);

        mBoxTxmId = findViewById(R.id.box_txm_id);
        mBoxTxmType = findViewById(R.id.box_txm_type);
        mBoxTxmName = findViewById(R.id.box_txm_name);
        mBoxTxmDesin = findViewById(R.id.box_txm_desin);
        mBoxTxmRowCole = findViewById(R.id.box_txm_row_cole);
        mBoxTxmLon = findViewById(R.id.box_txm_lon);
        mBoxTxmLat = findViewById(R.id.box_txm_lat);
        mBoxBdzNum = findViewById(R.id.box_bdz_num);
        mBoxByqNum = findViewById(R.id.box_byq_num);
        mBoxTqNum = findViewById(R.id.box_tq_num);
        mBoxXlNum = findViewById(R.id.box_xl_num);
        mBoxUserNum = findViewById(R.id.box_user_num);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BleManager.getInstance().disconnectAllDevice();
    }
}
